package cn.edu.tju.fctl.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * Created by catold on 15/12/17.
 */
public class VacationTypeUtil {
    public static final int VACATIONTYPENUM = 5;//out is not a sort of vacation.
    public static final int ANNUALLEAVE = 1, //年假
                            MATERNITYLEAVE = 2, // 产假
                            MARRIAGELEAVE = 3, // 婚假
                            CASUALLEAVE = 4, // 事假
                            SICKLEAVE = 5, // 病假
                            OUT = 6; //

    private volatile static VacationTypeUtil instance;
    private static final String SPLIT = ","; // 分隔符

    public static VacationTypeUtil getInstance() {
        if(instance == null){
            synchronized (VacationTypeUtil.class) {
                if(instance == null){
                    instance = new VacationTypeUtil();
                }
            }
        }
        return instance;
    }

    private List<VacationType> vacationTypes;

    private VacationTypeUtil(){
        initialize();
    }

    private void initialize(){
        vacationTypes = new ArrayList<VacationType>();

        List<VacationType> subTypes;

        VacationType yearVacaion = new VacationType(ANNUALLEAVE, "年假", 10);
        addVacationType(yearVacaion);

        VacationType chanjiaVacation = new VacationType(MATERNITYLEAVE, "产假", 10);
        subTypes = new ArrayList<VacationType>();
        subTypes.add(new VacationType(1, "难产", 20));
        subTypes.add(new VacationType(2, "剖腹产", 20));
        subTypes.add(new VacationType(3, "多胞胎", 20));
        subTypes.add(new VacationType(4, "双胞胎", 20));
        chanjiaVacation.setSubTypes(subTypes);
        addVacationType(chanjiaVacation);

        VacationType hunjia = new VacationType(MARRIAGELEAVE, "婚假", 10);
        addVacationType(hunjia);

        VacationType shijia = new VacationType(CASUALLEAVE, "事假", 0);
        subTypes = new ArrayList<VacationType>();
        subTypes.add(new VacationType(1, "事假", 0));
        shijia.setSubTypes(subTypes);
        addVacationType(shijia);

        VacationType bingjia = new VacationType(SICKLEAVE, "病假", 0);
        subTypes = new ArrayList<VacationType>();
        subTypes.add(new VacationType(1, "病假", 0));
        bingjia.setSubTypes(subTypes);
        addVacationType(bingjia);

        VacationType waichu = new VacationType(OUT, "外出", 0);
        subTypes = new ArrayList<VacationType>();
        subTypes.add(new VacationType(1, "外出", 0));
        waichu.setSubTypes(subTypes);
        addVacationType(waichu);
    }

    private void addVacationType(VacationType vacationType) {
        vacationTypes.add(vacationType);
    }

    /**
     * 通过id获取第一级假期信息
     * @param id
     * @return
     */
    public VacationType getVacationById(Integer id){
        for(VacationType vacationType : vacationTypes){
            if(vacationType.getId().equals(id)){
                return vacationType;
            }
        }
        return null;
    }

    /**
     * 获取子类型信息
     * @param pid
     * @param cid
     * @return
     */
    public VacationType getSubType(Integer pid, Integer cid) {
        for(VacationType vacationType : vacationTypes){
            if (vacationType.getId().equals(pid)) {
                return vacationType;
            }
        }
        return null;
    }

    /**
     * 判断第一级是否包括第二级
     * @param pid 第一级id
     * @param cid 第二级id
     * @return
     */
    public boolean isRelated(Integer pid, Integer cid) {
        for(VacationType vacationType : vacationTypes){
            if(vacationType.getId().equals(pid)){
                return vacationType.containsSubType(pid);
            }
        }
        return false;
    }

    /**
     * 通过第一级pid和第二级id拼接字符串构建type
     * @param pid  第一级pid
     * @param cidstr 第二级数据库格式 "1,2,3", 可为空
     * @return
     */
    public VacationType build(Integer pid, String cidstr){
        VacationType vacationType = getVacationById(pid);
        if(vacationType == null){
            return null;
        }

        String[] strs = cidstr.split(SPLIT);
        try {
            for(String str : strs){
                Integer id = Integer.parseInt(str);

                vacationType.addSubVacationType(vacationType.getSubType(id));
            }
        } catch (Exception e) {
//            return null;
        }

        return vacationType;
    }


    /**
     * 生成存于数据库的子类字符串
     * @return
     */
    public String generateSubTypeString(VacationType vacationType){
        String str = null;
        for(int i = 0; vacationType.getSubTypes() != null && i < vacationType.getSubTypes().size(); i++) {
            if(i == 0){
                str = vacationType.getSubTypes().get(i).getId().toString();
            } else {
                str += SPLIT + vacationType.getSubTypes().get(i).getId().toString();
            }
        }
        return str;
    }

    public List<VacationType> getVacationTypes(){
        return vacationTypes;
    }
}
